// import hex_md5 from './md5.js';
let hex_md5 = require('./md5.js');
import toutf from './base64.js';
let env = 'prd';
let thirdUrl, requestUrl, key, desKey,erpKey,erpLoginUrl,erpBizUrl;
if (env == 'prd'){
  thirdUrl = 'https://api.winhc.cn/miniapp/';
  requestUrl = 'https://api.winhc.cn/namecard/';
  erpLoginUrl = "https://api.winhc.cn/login/loginAction.htm";
  erpBizUrl = "https://api.winhc.cn/erp/bizAction.htm";
  key = 'iroolf958i09d0lg69fg';
  desKey = "F311E3F5D0E5B54D";
  erpKey = "iroolf958i09d0lg60df";

}else{
  thirdUrl = 'https://t.winhc.net/miniapp/';//http://106.14.81.247:9094/  https://t.winhc.net/miniapp/
  requestUrl = 'https://t.winhc.net/namecard/';//https://t.winhc.net/namecard/  http://10.1.10.172:8792/  http://106.14.81.247:8792/
  key = '123456';
  desKey = "0123456789012345";
  erpKey = "123456";
  erpLoginUrl = "https://t.winhc.net/login/loginAction.htm"; //http://10.1.10.172:10333/loginAction.htm  https://t.winhc.net/login/loginAction.htm
  erpBizUrl = "https://t.winhc.net/erp/bizAction.htm";//https://t.winhc.net/erp/bizAction.htm http://10.1.10.170:8029/bizAction.htm 
}

const appid = "wxb70cce7f9a8afb10";

const signType = "MD5";
const request_api = (res,fnScu,fnFail) =>{
  wx.request({
    url: thirdUrl +'sessionKey', //仅为示例，并非真实的接口地址
    method: 'GET',
    data: {
      code: res
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function (obj) {
        fnScu(obj);
    },
    fail: function (obj) {
      wx.showModal({
        title: '提示',
        content: '超时异常',
        showCancel: false,
        confirmText: "知道了",
        success: function (res) {
          if (res.confirm) {
            fnFail(obj)
          } else if (res.cancel) {
          }
        }
      })
    }
  })
}

const requestGetInfo = (serviceName, bodyInfo, fnScu, fnFail) =>{
  let signInfo = hex_md5(JSON.stringify(bodyInfo) + key);
  wx.request({
    url: requestUrl + serviceName, //仅为示例，并非真实的接口地址
    method: 'GET',
    data: {
      "serviceName": serviceName,
      "signType": signType,
      "sign": signInfo,
      "body": bodyInfo
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function (obj) {
      fnScu(obj);
    },
    fail: function (obj) {
      wx.showModal({
        title: '提示',
        content: '超时异常',
        showCancel: false,
        confirmText: "知道了",
        success: function (res) {
          if (res.confirm) {
            fnFail(obj)
          } else if (res.cancel) {
          }
        }
      })
    }
  })
}

const requestPostInfo = (serviceName, bodyInfo, fnScu, fnFail) => {
  let signInfo = hex_md5(JSON.stringify(bodyInfo) + key);
  wx.request({
    url: requestUrl + serviceName, //仅为示例，并非真实的接口地址
    method: 'POST',
    data: {
      "serviceName": serviceName,
      "signType": signType,
      "sign": signInfo,
      "body": JSON.stringify(bodyInfo)
    },
    header: {
      'content-type': 'application/x-www-form-urlencoded' // 默认值
    },
    success: function (obj) {
      fnScu(obj);
    },
    fail: function (obj) {
      wx.showModal({
        title: '提示',
        content: '超时异常',
        showCancel: false,
        confirmText: "知道了",
        success: function (res) {
          if (res.confirm) {
            fnFail(obj)
          } else if (res.cancel) {
          }
        }
      })
    }
  })
}

const requestUploadImg = (serviceName, bodyInfo, fnScu, fnFail) => {
  let signInfo = hex_md5(JSON.stringify(bodyInfo) + key);
  wx.uploadFile({
    url: requestUrl + serviceName, //仅为示例，非真实的接口地址
    filePath: bodyInfo.path,
    name: 'file',
    formData: {
      sign: signInfo,
      body: JSON.stringify(bodyInfo)
    },
    success: function (obj) {
      fnScu(obj);
    },
    fail: function (obj) {
      wx.showModal({
        title: '提示',
        content: '超时异常',
        showCancel: false,
        confirmText: "知道了",
        success: function (res) {
          if (res.confirm) {
            fnFail(obj)
          } else if (res.cancel) {
          }
        }
      })
    }
  })
}

const requestLogin_api = (serviceName,headerInfo,bodyInfo,fnScu,fnFail) => {
  let gzhUrl;
  if (serviceName == "userLogin" || serviceName == "sendCheckNo"){
    gzhUrl = erpLoginUrl;
    if (serviceName == "userLogin" ){
      if (bodyInfo.loginType == "password"){
        bodyInfo.password = hex_md5(bodyInfo.password + desKey);
      }
    }
  }else{
    gzhUrl = erpBizUrl;
  }
  let commonData = {
    serviceName: serviceName,
    version: "1.0",
    appid: "YHC_MP",
    signType: "MD5",
    pageNum: headerInfo.pageNum,
    pageSize: headerInfo.pageSize,
    sessionId: headerInfo.sessionId,
    body: bodyInfo
  }
  let signSource = commonData.appid + commonData.serviceName + commonData.version + commonData.sessionId + JSON.stringify(commonData.body) + erpKey;
  // console.log("signSource===", signSource)
  let signInfo = hex_md5(signSource);
  // console.log("signInfo======", signInfo)
  commonData.sign = signInfo;
  wx.request({
    url: gzhUrl, //仅为示例，并非真实的接口地址
    method: 'GET',
    data: commonData,
    header: {
      'content-type': 'application/json;charset=UTF-8' // 默认值
    },
    success: function (obj) {
      if (obj.data.isSuccess == 'F' && obj.data.errorCode == 'ERROR_USER_NOT_LOGIN') {
        wx.hideLoading();
        wx.showModal({
          title: '用户未登录',
          content: '是否去登陆',
          // showCancel: false,
          confirmText: "确定",
          success: function (res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '/pages/reg/reg',
              })
            } else if (res.cancel) {
              wx.navigateBack();
            }
          }
        })
      } else {
        fnScu(obj);
      }
      
    },
    fail: function (obj) {
      // console.log("调用登录失败=", obj)
      wx.showModal({
        title: '提示',
        content: '超时异常',
        showCancel:false,
        confirmText:"知道了",
        success: function (res) {
          if (res.confirm) {
            fnFail(obj)
          } else if (res.cancel) {
          }
        }
      })
    }
  })
}

// queryApi: function(token){
  //   console.log("token====", token)
  //   wx.request({
  //     url: 'https://tmis.winhc.net/projectBaseInfos', //仅为示例，并非真实的接口地址
  //     method: 'GET',
  //     data: {
  //       body: {},
  //       pageNum: '1',
  //       pageSize: '10'
  //     },
  //     header: {
  //       'content-type': 'application/json;charset=UTF-8', // 默认值
  //       "Auth": "Bearer" + token
  //     },
  //     success: function (obj) {
  //       console.log("调用mis查询成功=", obj)
  //     },
  //     fail: function (obj) {
  //       console.log("调用mis查询失败=", obj)
  //     }
  //   })
  // }

  // //测试调用mis接口
  // tesMisApi: function(){
  //   let _this = this;
  //   wx.request({
  //     url: 'https://tmis.winhc.net/token', //仅为示例，并非真实的接口地址
  //     method: 'POST',
  //     data: {
  //       loginId: 'hxm',
  //       password: '111111'
  //     },
  //     header: {
  //       'content-type': 'application/json;charset=UTF-8' // 默认值
  //     },
  //     success: function (obj) {
  //       console.log("调用mis成功=", obj)
  //       _this.queryApi(obj.data.token);
  //     },
  //     fail: function (obj) {
  //       console.log("调用mis失败=",obj)
  //     }
  //   })
  // },


module.exports = {
  request_api: request_api,
  get_api: requestGetInfo,
  post_api: requestPostInfo,
  requestUploadImg:requestUploadImg,
  request_url:thirdUrl,
  requestLogin_api: requestLogin_api
};